package cn.psvmc.cxzapi.util.delivery;


import cn.psvmc.cxzapi.model.delivery.DbOrderCreate;
import cn.psvmc.cxzapi.model.delivery.DbOrderSearch;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 德邦物流对接（https://dpopen.deppon.com/#/home）
 * @Author: YWX
 * @Date 2024/8/18 16:57
 **/
@Slf4j
public class DbAPIService {

    public static final Map<String, String> STATUS_MAP;

    static {
        STATUS_MAP = DbAPIService.createStatusMap();
    }

    /**
     * @Description 创建操作状态映射信息
     * @Date 2024/9/19 1:00
     * @Author YWX
     * @Param []
     * @Return java.util.Map<java.lang.String, java.lang.String>
     **/
    public static Map<String, String> createStatusMap() {
        Map<String, String> statusMap = new HashMap<>();
        statusMap.put("GOT", "开单");
        statusMap.put("DEPARTURE", "出站");
        statusMap.put("ARRIVAL", "进站");
        statusMap.put("ADVANCE_DELIVERY", "预派送");
        statusMap.put("SENT_SCAN", "派送");
        statusMap.put("ERROR", "滞留,延时派送");
        statusMap.put("FAILED", "客户拒签/运单作废");
        statusMap.put("SIGNED", "签收");
        statusMap.put("BACK_SIGNED", "退回件签收");
        statusMap.put("OPERATETRACK", "转寄");
        statusMap.put("STA_INBOUND", "快递员入柜");
        statusMap.put("STA_SIGN", "用户提货（快递柜签收）");
        statusMap.put("ADVANCE_NOTICE", "提前通知");
        return statusMap;
    }

    /**
     * @Description 获取运单状态
     * @Date 2024/10/21 15:26
     * @Author YWX
     * @Param [status]
     * @Return java.lang.String
     **/
    public static String getDeliveryStatus(String status) {
        if (DbAPIService.STATUS_MAP.containsKey(status)) {
            return DbAPIService.STATUS_MAP.get(status);
        }
        return status;
    }

    /**
     * @Description 查看订单
     * @Date 2024/8/18 16:54
     * @Author YWX
     * @Param [param]
     * @Return java.lang.String
     **/
    public static String checkOrder(DbOrderSearch param) {
        //订单内容 json字符串，SDK提供FastJsonUtil转Json
        //TODO 德邦
//        String params = FastJsonUtil.toJSONString(param);
//        return DbAPIService.sendRequest(params, ConfigKey.queryOrder);
        return "";
    }

    /**
     * @Description 新标准轨迹查询
     * @Date 2024/9/19 9:14
     * @Author YWX
     * @Param [param]
     * @Return java.lang.String
     **/
    public static String newTraceQuery(String mailNo) {
        //订单内容 json字符串，SDK提供FastJsonUtil转Json
        Map<String, String> param = new HashMap<>();
        param.put("mailNo", mailNo);
//        String params = FastJsonUtil.toJSONString(param);
//        return DbAPIService.sendRequest(params, ConfigKey.newTraceQuery);
        return "";
    }

    /**
     * @Description 标准轨迹订阅
     * @Date 2024/9/19 14:22
     * @Author YWX
     * @Param [orderNumber, trackingNumber]
     * @Return java.lang.String
     **/
    public static void standTraceSubscribe(String orderNumber, String trackingNumber) {
        //订单内容 json字符串，SDK提供FastJsonUtil转Json
        Map<String, String> param = new HashMap<>();
        param.put("order_number", orderNumber);
        param.put("tracking_number", trackingNumber);//优先使用运单号订阅，订单号可以不传；运单号和订单号不能同时为空
        //todo
//        String params = FastJsonUtil.toJSONString(param);
//        DbAPIService.sendRequest(params, ConfigKey.standTraceSubscribe);
    }

    /**
     * @Description 新增订单
     * @Date 2024/9/7 21:43
     * @Author YWX
     * @Param [param]
     * @Return java.lang.String
     * {
     *     "result": "true",
     *     "reason": "成功",
     *     "mailNo": "DPK6778652685710,DPK6779202168551",
     *     "logisticID": "ULUIP3391113",
     *     "arrivedOrgSimpleName": "上海上海市",
     *     "resultCode": "1000",
     *     "parentMailNo": "DPK6427263330134",
     *     "uniquerRequestNumber": "18196236167630674"
     * }
     **/
    public static String createOrder(DbOrderCreate param) {
        //TODO
//        String params = FastJsonUtil.toJSONString(param);
//        return DbAPIService.sendRequest(params, ConfigKey.createOrderNotify);
        return "";
    }

    /**
     * @Description 发送请求
     * @Date 2024/9/7 22:48
     * @Author YWX
     * @Param [params, method]
     * @Return java.lang.String
     **/
//    public static String sendRequest(String params, String method) {
//        //订单内容 json字符串，SDK提供FastJsonUtil转Json
//        //String params = FastJsonUtil.toJSONString(param);
//        //companyCode与appkey为双方约定
//        String companyCode = ConfigKey.companyCode;
//        String appkey = ConfigKey.appkey;
//        //时间戳 SDK提供SecurityUtil获取时间戳
//        String timestamp = SecurityUtil.getTimestamp();
//        //摘要 SDK提供SecurityUtil生成摘要
//        String digest = SecurityUtil.getDigest(params + appkey + timestamp);
//        //post请求参数
//        NameValuePair[] data = new NameValuePair[4];
//        data[0] = new NameValuePair("companyCode", companyCode);
//        data[1] = new NameValuePair("digest", digest);
//        data[2] = new NameValuePair("timestamp", timestamp);
//        data[3] = new NameValuePair("params", params);
//        //请求url
//        String url = ConfigKey.requestUrl;
//        //返回结果
//        try {
//            String response = HttpUtils.sendRequest(url + method, data, "UTF-8", 5000);
//            DbAPIService.log.info("德邦物流接口请求参数：{}", params);
//            DbAPIService.log.info("德邦物流接口请求结果：{}", response);
//            return response;
//        } catch (IOException e) {
//            DbAPIService.log.error("德邦物流接口请求异常：{}", e.getMessage());
//            throw new ServiceException(e.getMessage());
//        }
//    }

}
